home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2005 June / DPPCPRO0605A.ISO / Editorial / Programming with Delphi 2005 / Delphi 2005 1 / calcvat / vatform.pas < prev   
Encoding:
Pascal/Delphi Source File  |  2005-02-28  |  3.6 KB  |  133 lines

  1. unit Vatform;
  2.  
  3. interface
  4.  
  5. uses
  6.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  7.   Forms, Dialogs, StdCtrls, XPMan;
  8.  
  9. type
  10. // Note the definition of the form TForm1 is entered by Delphi and this
  11. // code should not be altered
  12.   TForm1 = class(TForm)
  13.     CalcBtn: TButton;
  14.     GroupBox1: TGroupBox;
  15.     SubTotRB: TRadioButton;
  16.     GrandTotRB: TRadioButton;
  17.     SubTotal: TEdit;
  18.     Vat: TEdit;
  19.     GrandTotal: TEdit;
  20.     Label1: TLabel;
  21.     XPManifest1: TXPManifest;
  22.     procedure CalcBtnClick(Sender: TObject);
  23.     procedure SubTotRBClick(Sender: TObject);
  24.     procedure GrandTotRBClick(Sender: TObject);
  25.   private
  26.     { Private declarations }
  27.   public
  28.     { Public declarations }
  29.   end;
  30.  
  31. var
  32.   Form1: TForm1;
  33.  
  34. implementation
  35.  
  36. {$R *.DFM}
  37.  
  38. procedure InputError( TE: TEdit; errcode : integer );
  39. // given an integer, errcode, and an edit box, TE, this procedure selects
  40. // (that is, it highlights) the character at the index given by the TE.SelStart
  41. // property. It slao displays the character in an error message
  42. var
  43.    Msg : string;
  44. begin
  45.     if TE.Text = '' Then
  46.        Msg := 'You must enter a value'
  47.     else
  48.        Msg :=  'Invalid character: ' + Copy(TE.Text, errcode, 1);
  49.     MessageDlg(Msg, mtError,
  50.             [mbOk], 0);
  51.     TE.SetFocus;
  52.     TE.SelStart := errcode-1;
  53.     TE.SelLength := 1;
  54. end;
  55.  
  56. procedure PlusVat( var subtot, vattot, grandtot : real );
  57. // given real value subtot, calculates VAT and assigns this to
  58. // vattot. Calculates VAT inclusive total and assigns this to
  59. // granddot. Note that all 3 input arguments to this procedure
  60. // are preceded by the VAR keyword. This means that they are passed
  61. // 'by reference' so that any changes made inside the procuedure affect
  62. // the original variables that were passed to the procedure.
  63. begin
  64.    vattot := (subtot * 0.175);
  65.    grandtot := vattot + subtot;
  66. end;
  67.  
  68. procedure MinusVat( var subtot, vattot, grandtot : real );
  69. // given real value grandtot, calculates VAT and assigns this to
  70. // vattot. Calculates VAT exclusive total and assigns this to
  71. // subdot.
  72. begin
  73.    vattot := (grandtot * 0.14894);
  74.    subtot := grandtot - vattot;
  75. end;
  76.  
  77. procedure TForm1.CalcBtnClick(Sender: TObject);
  78. var
  79.    stxt, vtxt,
  80.        gtxt : string;
  81.    st, vt, gt  : real;
  82.    errcode : integer;
  83. begin
  84.    st := 0.0;
  85.    vt := 0.0;
  86.    gt := 0.0;
  87.    stxt := '';
  88.    vtxt := '';
  89.    gtxt := '';
  90.    // These if...else blocks take different actions according to which
  91.    // radio button is checked on the form. If the SubTotRB button is
  92.    // checked, the grandtotal is calculated from the subtotal which the
  93.    // user has entered. Otherwise, the subtotal is calculated from
  94.    // the grand total which has been entered
  95.    if SubTotRB.Checked Then
  96.    begin
  97.        Val(SubTotal.Text, st, errcode);
  98.        if errcode <> 0 then InputError(SubTotal,
  99.            errcode);
  100.        begin
  101.           PlusVat(st,vt,gt);
  102.           Str(vt:2:2, vtxt );
  103.           Str(gt:2:2, gtxt );
  104.           Vat.Text := vtxt;
  105.           GrandTotal.Text := gtxt;
  106.        end;
  107.    end // note: no semi-colon prior to the 'else' keyword
  108.    else
  109.    begin
  110.        Val(GrandTotal.Text, gt, errcode);
  111.        if errcode <> 0 then InputError( GrandTotal, errcode );
  112.        begin
  113.           MinusVat(st,vt,gt);
  114.           Str(vt:2:2, vtxt );
  115.           Str(st:2:2, stxt );
  116.           Vat.Text := vtxt;
  117.           SubTotal.Text := stxt;
  118.        end;
  119.    end;
  120. end;
  121.  
  122. procedure TForm1.SubTotRBClick(Sender: TObject);
  123. begin
  124.      CalcBtn.Caption := '&Calculate the Grand Total';
  125. end;
  126.  
  127. procedure TForm1.GrandTotRBClick(Sender: TObject);
  128. begin
  129.      CalcBtn.Caption := '&Calculate the Sub Total';
  130. end;
  131.  
  132. end.
  133.